package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class ProcurementTypeListAdapter extends RecyclerView.Adapter<ProcurementTypeListAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ProcurementTypeListBeen.DataBean> list;
    private Context context;


    public ProcurementTypeListAdapter(Context context, List<ProcurementTypeListBeen.DataBean> list) {
        this.list = list;
        this.context = context;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_procurement_type_list, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        holder.tvTypeName.setText(list.get(position).getCategory_name());
        if (list.get(position).isIs_open()) {
            holder.ivIsOpen.setImageResource(R.drawable.icon_cai_jiantou_bi);
            holder.llHint.setVisibility(View.VISIBLE);
            holder.rvDetailsList.setVisibility(View.VISIBLE);
        } else {
            holder.ivIsOpen.setImageResource(R.drawable.icon_cai_jiantou_kai);
            holder.llHint.setVisibility(View.GONE);
            holder.rvDetailsList.setVisibility(View.GONE);
        }
        ProcurementTypeListItemAdapter procurementTypeListAdapter = new ProcurementTypeListItemAdapter(context, list.get(position).getGoods_list(),this,position);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvDetailsList.setLayoutManager(linearLayoutManager);
        holder.rvDetailsList.setItemAnimator(new DefaultItemAnimator());
        holder.rvDetailsList.setNestedScrollingEnabled(false);
        holder.rvDetailsList.setAdapter(procurementTypeListAdapter);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setIs_open(!list.get(position).isIs_open());
                notifyDataSetChanged();
            }
        });

        holder.tvTypeNum.setText(list.get(position).getGoods_list().size()+"");
        int select_type_num = 0;
        int jian_num=0;
        for (ProcurementTypeListBeen.DataBean.GoodsListBean goodListbeen : list.get(position).getGoods_list()) {
           if (goodListbeen.isIs_select()){
               select_type_num++;
               jian_num+= goodListbeen.getGoods_num();
           }
        }
        holder.tvSelectTypeNum.setText(select_type_num+"");
        holder.tvSelectJianNum.setText(jian_num+"");
    }





    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.iv_is_open)
        ImageView ivIsOpen;
        @BindView(R.id.ll_hint)
        LinearLayout llHint;
        @BindView(R.id.rv_details_list)
        RecyclerView rvDetailsList;
        @BindView(R.id.tv_type_num)
        TextView tvTypeNum;
        @BindView(R.id.tv_select_type_num)
        TextView tvSelectTypeNum;
        @BindView(R.id.tv_select_jian_num)
        TextView tvSelectJianNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
